package com.wzw.shoppingcart.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzw.shoppingcart.entity.CartLine;
import com.wzw.shoppingcart.entity.Product;
import com.wzw.shoppingcart.entity.Promotion;
import com.wzw.shoppingcart.mapper.PromotionMapper;
import com.wzw.shoppingcart.service.ProductService;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 *@author wzw
 * 2023/4/14
 */
@Service
@Setter(onMethod_ = {@Autowired})
public class PromotionService extends ServiceImpl<PromotionMapper, Promotion> {

    private ProductService productService;


    public boolean exist(Integer productId, String discountType) {
        return lambdaQuery().eq(Promotion::getProductId, productId)
                .eq(Promotion::getDiscountType, discountType)
                .exists();
    }


    /**
     * 得到购物车中参与某项活动的商品列表.
     */
    public List<Product> promotionProduct(List<CartLine> cartLines, String discountType) {
        List<Integer> productIds = cartLines.stream()
                .map(CartLine::getProductId).collect(Collectors.toList());

        List<Promotion> promotionList = lambdaQuery()
                .in(Promotion::getProductId, productIds)
                .eq(Promotion::getDiscountType, discountType)
                .list();

        return promotionList.stream()
                .map(p -> productService.getProductById(p.getProductId()))
                .collect(Collectors.toList());
    }

}
